# Maintainer: Jonathan Armstrong <jda2158@gmail.com>

_realname=flashrom
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2
pkgrel=3
pkgdesc='Utility for detecting, reading, writing, verifying and erasing flash chips (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://www.flashrom.org/Flashrom'
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-libftdi"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
# Use upstream source tarball for versioninfo.inc file
source=("https://download.flashrom.org/releases/${_realname}-v${pkgver}.tar.bz2"{,.asc}
        0001-meson-check-version-script.patch)
sha256sums=('e1f8d95881f5a4365dfe58776ce821dfcee0f138f75d0f44f8a3cd032d9ea42b'
            'SKIP'
            '71e248541d0aed0f01b38dccdab257e3cf6e6d43513cf82a9f99812ac04271ef')
validpgpkeys=('58A4868B25C7CFD662FB0132A3EB95B8D9780F68') # David Hendricks (packaging key) <david.hendricks@gmail.com>

prepare() {
  cd "${_realname}-v${pkgver}"

  # Workaround: DEF file should be used instead of MAP file
  patch -p1 -i "${srcdir}/0001-meson-check-version-script.patch"
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    meson \
      -Dpciutils=false \
      -Dconfig_linux_mtd=false \
      -Dconfig_linux_spi=false \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      ../${_realname}-v${pkgver}

  meson compile
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  # TODO: check once master branch gets numbered release (i.e., tests/)
  meson test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" meson install

  # To enable usage relative paths for next operations
  cd "${pkgdir}"/${MINGW_PREFIX}

  # Move content to standard dirs
  mv -v sbin/flashrom.exe bin/

  install -Dm644 "${srcdir}/${_realname}-v${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
 
# echo replace USB-UART drivers (if needed) with WinUSB (libusb) using zadig [https://zadig.akeo.ie/]
}
